/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.commands.queries;

import com.bionic.ce.beans.Query;
import com.bionic.ce.businesslogic.QueryService;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Jarik
 */
public class CheckActiveQueries {

    private static Query query = new Query();

    private static void expirationCheck() {
        query.setStatus("Active");
        List<Query> list = QueryService.returnActiveQueries();
        for (Query q : list) {
            if ((q.getFinishDate()).before(new Date())) {
                q.setStatus("Expired");
                QueryService.updateCurrentQuery(q);
            }
        }
    }

    public static List<Query> check() {
        expirationCheck();
        List<Query> list = QueryService.returnActiveQueries();
        return list;
    }

    public static List<Query> check(Integer userId) {
        expirationCheck();
        List<Query> list = QueryService.returnActiveQueries(userId);
        return list;
    }

    public static Double balancecheck(Integer userId, String currencyFrom) {
        Double sum = 0.0;
        Query query = new Query();
        query.setOpenByUser(userId);
        query.setCurrencyFrom(currencyFrom);
        query.setStatus("Active");
        List<Query> list = QueryService.findByDynamicSelect(query);
        for (Query q : list) {
            sum += q.getAmount();
        }
        return sum;
    }
}
